#pragma once

#include<cstdio>
#include<iostream>
#include<vector>
#include<unistd.h>
#include<cstring>
#include<fstream>
#define PATH "./data.txt"
#define PATHB "./data.bin"

struct Contact
{
    Contact()
    {}
    Contact(const std::string &name, const std::string &phoneNumber)
        : name(name), phoneNumber(phoneNumber)
    {
    }
    std::string name;
    std::string phoneNumber;
};


class AddressBook
{
public:
    void addContact(const std::string &name, const std::string &phoneNumber)
    {
        Contact newContact(name, phoneNumber);
        contacts.push_back(newContact);
        std::cout << "通讯录添加成功！.\n";
    }

    bool Loadfile_txt()
    {
        std::ifstream in(PATH);
        if(!in.is_open()) return false;
        Contact temp;
        while (in.read((char *)&temp, sizeof(temp)))
        {
            contacts.push_back(temp);
        }

        true;
    }

    bool loadfile_binary()
    {
        std::ifstream in(PATH, std::ios::binary);
        if (!in.is_open()) return false;

        Contact temp;
        in.read((char *)&t, sizeof(temp));
        contacts.push_back(temp);

        return true;
    }

    void Save_txt()
    {

    }

    void Save_binary()
    {

    }

    void displayContacts() const
    {
        if (contacts.empty())
        {
            std::cout << "通讯录为空！.\n";
        }
        else
        {
            std::cout << "请输入你们的名字和电话！\n";
            for (const auto &contact : contacts)
            {
                std::cout << "姓名: " << contact.name << ", 电话: " << contact.phoneNumber << "\n";
            }
        }
    }

private:
    std::vector<Contact> contacts;
};


